package com.tx.studentManager.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.NamedFilterList;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.common.format.util.DateUtil;
import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.model.Page;
import com.tx.studentManager.model.Role;
import com.tx.studentManager.model.UrlFilter;
import com.tx.studentManager.qo.UrlFilterQO;
import com.tx.studentManager.service.RoleService;
import com.tx.studentManager.service.UrlFilterService;
import com.tx.studentManager.utils.DWZJsonUtil;
/**
 * 
 * @描述 权限控制控制器，权限票配置
 * @since studentManager-admin 应用程序版本号
 * @version 1.0 2015-9-24 下午4:11:19
 * @author 此生唯晴
 */
@RequestMapping(value = "/perm")
@Controller
public class AuthController {
	@Autowired
	private AbstractShiroFilter shiroFilter;
	@Autowired
	private SystemConfig systemConfig;
	@Autowired
	private UrlFilterService urlFilterService;
	@Autowired
	private RoleService roleService;
	/**
	 * 
	 * @描述 无权限调转页面 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/noPerm", produces = "text/html;charset=UTF-8")
	public String noPermission() {
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "您没有访问权限", null, null);
	}
	/**
	 * 
	 * @描述 显示当前url权限控制链
	 * @return
	 */
	@RequestMapping(value="/showChain")
	public ModelAndView showChain(){
		PathMatchingFilterChainResolver filterChainResolver=(PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
		DefaultFilterChainManager filterChainManager=(DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
		Map<String, NamedFilterList> chains=filterChainManager.getFilterChains();
		Set<String> chainNames=filterChainManager.getChainNames();
		Map<String, Filter> filters=filterChainManager.getFilters();
		ModelAndView modelAndView=new ModelAndView("auth/showChain");
		modelAndView.addObject("chains", chains);
		modelAndView.addObject("chainNames", chainNames);
		modelAndView.addObject("filters",filters);
		return modelAndView;
	}
	
	@RequestMapping(value="/list")
	public ModelAndView list(@ModelAttribute UrlFilterQO qo,
			@RequestParam(required = false) String createDateStartStr,
			@RequestParam(required = false) String createDateEndStr,
			@RequestParam(required = false, value = "numPerPage") Integer pageSize,
			@RequestParam(required = false, value = "pageNum") Integer pageNo){
		if (StringUtils.isNotBlank(createDateEndStr)) {
			qo.setCreateDateEnd(DateUtil.str2DateTime(createDateEndStr));
		}
		if (StringUtils.isNotBlank(createDateStartStr)) {
			qo.setCreateDateStart(DateUtil.str2DateTime(createDateStartStr));
		}
		pageSize = pageSize == null ? systemConfig.getQueryPageSize() : pageSize;
		pageNo = pageNo == null ? 1 : pageNo;
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setCondition(qo);
		page=urlFilterService.list(page);
		List<Role> roleList=roleService.findAll();
		ModelAndView modelAndView=new ModelAndView("auth/listPerm");
		modelAndView.addObject("page", page);
		modelAndView.addObject(roleList);
		return modelAndView;
	}
	

	/**
	 * 
	 * @描述 添加权限过滤
	 * @return 权限添加视图
	 */
	@RequestMapping(value = "/add")
	public ModelAndView add(){
		ModelAndView  modelAndView=new ModelAndView("auth/add");
		List<Role> roleList=roleService.findAll();
		modelAndView.addObject(roleList);
		return modelAndView;
	}
	/**
	 * @描述 添加权限控制规则
	 * @param urlFilter
	 * @return
	 */
	@RequestMapping(value = "/toAdd",produces = "text/html;charset=UTF-8")
	public @ResponseBody String toAdd(@ModelAttribute UrlFilter urlFilter){
		urlFilter=urlFilterService.insert(urlFilter);
		if (urlFilter==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "操作失败！", null, null);
		}
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "添加成功！", DWZJsonUtil.OPER_CLOSE_CURRENT, "authorityManager");
	}
	
	@RequestMapping("/update")
	public ModelAndView update(@RequestParam(required=true) String id){
		UrlFilter urlFilter=urlFilterService.findById(id);
		
		ModelAndView modelAndView=new ModelAndView("auth/update");
		modelAndView.addObject(urlFilter);
		List<Role> roleList=roleService.findAll();
		modelAndView.addObject(roleList);
		return modelAndView;
	}
	/**
	 * 
	 * @描述 修改权限过滤规则 
	 * @param urlFilter
	 * @return
	 */
	@RequestMapping(value = "/toUpdate",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toUpdate(@ModelAttribute UrlFilter urlFilter,@RequestParam(required=false) String createDateStr){
		urlFilter.setCreateDate(DateUtil.str2DateTime(createDateStr));
		if (!urlFilterService.update(urlFilter)) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "修改失败！", null, null);
		}
		
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "修改成功！", DWZJsonUtil.OPER_CLOSE_CURRENT, "authorityManager");
	}
	
	/**
	 * 
	* @描述 启用禁用 
	* @param id
	* @param oper
	* @return
	 */
	@ResponseBody
	@RequestMapping(value="/useOrNot",produces = "text/html;charset=UTF-8")
	public String useOrNot(@RequestParam(required = true)String id,@RequestParam(required = true)Integer oper){
		UrlFilter urlFilter=urlFilterService.findById(id);
		if (urlFilter==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "该规则已不存在！", null, null);
		}
		urlFilter.setIsActived(oper);
		boolean result=urlFilterService.update(urlFilter);
		//操作失败
		if (!result) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "操作失败", null, null);
		}
		//根据操作返回提示信息
		if (oper==0) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "禁用成功！", null, null);
		}else {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "启用成功！", null, null);
		}
	}
	/**
	 * 
	 * @描述 删除权限控制操作 
	 * @param id 权限id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete",produces = "text/html;charset=UTF-8")
	public String delete(@RequestParam(required=true) String id){
		UrlFilter urlFilter=urlFilterService.load(id);
		if (urlFilter==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "删除对象不存在！", null, null);
		}
		if(urlFilterService.delete(id)){
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "操作成功！", null, "authorityManager");
		}else{
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "操作失败！", null, null);
		}
		
	}
}
